
var data = [];

$(function() {	
	Tip.emp('负责人');
	
	Tip.supplier();
});

var Process = {
	dialog_add_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {	
			"关闭": function() { 
				$(this).dialog("close");
			},			
			
			"添加": function() {				
				if (!confirm("确认是否添加？")) {
					return false;
				}				
	
				var form = $("#form_add")[0];
	
				var body = { };
				
				body.supplier = form.supplier.value;
				body.supplier_x = Supplier.supplier_x(form);
				
				body.name = form.name.value;
				body.content = form.content.value;				
				
				body.amount = form.amount.value;
				body.currency = form.currency.value;
												
				body.emp = form.emp.value;
				body.date = form.date.value;
				
				// body.item = data;
				body.item = $.toJSON(data);
				
				$(this).dialog("close");
	
				$.post('mold.php?do=Process.addPost', body, function(data) {
					$(result).html(data);
				});
			}
		});
	
		dialog_add(url, title, xy);
	},
	
	popup: function (e, p2, id, out, status) {
		$.cookie('id_', id);

		p2 = query(p2);
		$.cookie('p2', p2);

		var c1 = 'mold.php?do=Process.edit&id=' + id;
		var c2 = 'mold.php?do=Process.view&id=' + id;
		
		var cs = 'mold.php?do=Process.status&id=' + id + p2;
		
		var a1 = ['编辑信息', 'url', 'Process.dialog_edit_', '700x450'];	
		var a2 = ['查看信息', 'url', 'Common.dialog_view_', '650x420'];
			
		if (status == '1') {
			var d1 = 'mold.php?do=Process.verify&id=' + id;
			var b1 = ['审核报销', 'url', 'Process.dialog_verify', '650x400'];
			
			var s = d1 + ", " + c1 + ", " + c2;

			return this.popup_(e, [b1, a1, a2], s);
		}
		
		if (status == '2') {
			var b1 = ['开始外发', 'url', 'Process.list_start'];
			
			var d2 = 'mold.php?do=Process.finish&id=' + id;
			var b2 = ['完成外发', 'url', 'Process.dialog_finish', '650x400'];
			
			var s = cs + "&status=3, " + d2 + ", " + c1 + ", " + c2;

			return this.popup_(e, [b1, b2, a1, a2], s);
		}		
		
		if (status == '3') {
			var d1 = 'mold.php?do=Process.finish&id=' + id;
			var b1 = ['完成外发', 'url', 'Process.dialog_finish', '650x400'];
			
			var s = d1 + ", " + c2;

			return this.popup_(e, [b1, a2], s);
		}
		
		if (status == 6 && (out == 0 || out == null || out == '')) {
			var d1 = 'mold.php?do=Process.out&id=' + id;		
			var b1 = ['添加支出', 'url', 'Process.dialog_out', '640x400'];
						
			var s = d1 + ", " + c2;

			return this.popup_(e, [b1, a2], s);
		}

		var s = c2;

		return this.popup_(e, [a2], s);
	},

	popup_: function (e, a, s) {
		var id = $.cookie('id_');
		
		var c3 = 'mold.php?do=Process.copy&id=' + id;
		var a3 = ['复制信息', 'url', 'Process.dialog_copy_', '700x450'];
		
		var ss = 'mold.php?do=Process.ss&id=' + id;
		var bb = ['修改状态', 'url', 'Process.dialog_ss', '400x200'];
		
		s += ", " + c3 + ", " + ss;
		
		a.push(a3);
		a.push(bb);
		
		return popup(e, a, s);
	},

	/**
	 * 修改状态
	 */
	dialog_ss: function (url, title, xy) {
		result = Common.result();
		$.getJSON('mold.php?do=Process.ajax_oper', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"修改": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_edit")[0];

						var body = {};
						body.id = form.id.value;
						body.field = form.field.value;
						body.data = form.data.value;
						body.status = form.status.value;

						$(this).dialog("close");

						var url = 'mold.php?do=Process.ssPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	},

	dialog_edit_: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('mold.php?do=Process.ajax_oper', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {			
					"关闭": function() { 
						$(this).dialog("close");
					},
					
					"提交": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}
			
						var form = $("#form_edit")[0];
						
						var body = { };
						body.id = form.id.value;
						body.supplier = form.supplier.value;
						body.supplier_x = Supplier.supplier_x(form);
						
						body.name = form.name.value;
						body.content = form.content.value;				
						
						body.amount = form.amount.value;
						body.currency = form.currency.value;
														
						body.emp = form.emp.value;
						body.date = form.date.value;
						
						// body.item = data;
						body.item = $.toJSON(data);
						
						$(this).dialog("close");
			
						var url = 'mold.php?do=Process.editPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}
			
						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});
			
				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	},
	
	dialog_copy_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_edit').dialog('option', 'buttons', {			
			"关闭": function() { 
				$(this).dialog("close");
			},
			
			"复制": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}
	
				var form = $("#form_edit")[0];
				
				var body = { };
				body.supplier = form.supplier.value;
				body.supplier_x = Supplier.supplier_x(form);
				
				body.name = form.name.value;
				body.content = form.content.value;				
				
				body.amount = form.amount.value;
				body.currency = form.currency.value;
												
				body.emp = form.emp.value;
				body.date = form.date.value;
				
				// body.item = data;
				body.item = $.toJSON(data);
				
				$(this).dialog("close");
	
				var url = 'mold.php?do=Process.addPost';
	
				$.post(url, body, function(data) {
					$(result).html(data);
				});
			}
		});
	
		dialog_edit(url, title, xy);
	},
	
	dialog_verify: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('mold.php?do=Process.ajax_oper', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() { 
						$(this).dialog("close");
					},		

					"不同意": function() {
						if (!confirm("确认是否不同意外发加工？")) {
							return false;
						}				
						
						var form = $("#form_edit")[0];
						
						var body = {};
						body.id = form.id.value;
						body.content = form.content.value;
						body.status = 9;
						
						$(this).dialog("close");
						
						var url = 'mold.php?do=Process.verifyPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					},
					
					"同意": function() {
						if (!confirm("确认是否同意外发加工？")) {
							return false;
						}				

						var form = $("#form_edit")[0];
						
						var body = {};
						body.id = form.id.value;
						body.content = form.content.value;
						body.status = 2;
						
						$(this).dialog("close");

						var url = 'mold.php?do=Process.verifyPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});	
	},
	
	dialog_finish: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('mold.php?do=Process.ajax_oper', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() { 
						$(this).dialog("close");
					},
					
					"完成": function() {
						if (!confirm("确认是否完成外发加工？")) {
							return false;
						}				

						var form = $("#form_edit")[0];
						
						var body = {};
						body.id = form.id.value;
						body.content = form.content.value;
						
						$(this).dialog("close");

						var url = 'mold.php?do=Process.finishPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});	
	},
		
	list_start: function (url, view) {
		$.getJSON('mold.php?do=Process.ajax_oper', function(json) {
			if (json.status == 1) {
				if (confirm("是否要开始外发加工？")) {
					list(url, view);
				}
			} else {
				_error();
			}
		});
	},
	
	dialog_out: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {
			"关闭": function() { 
				$(this).dialog("close");
			},
			
			"提交": function() {
				if (!confirm("确认是否添加支出信息？")) {
					return false;
				}

				var form = $("#form_add")[0];
						
				var body = { };
				body.id = form.id.value;
				body.supplier = form.supplier.value;				
				body.supplier_x = Supplier.supplier_x(form);
				
				body.name = form.name.value;
				body.type = form.type.value;
				
				body.currency = form.currency.value;
				body.amount = form.amount.value;
				body.rate = form.rate.value;
				body.rmb = form.rmb.value;
				
				body.date = form.date.value;
				body.emp = form.emp.value;
				body.content = form.content.value;

				$(this).dialog("close");
				
				var url = 'mold.php?do=Process.outPost';
				var p2 = $.cookie('p2');
				if (p2 != null) {
					url += p2;
				}

				$.post(url, body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_add(url, title, xy);
	},
		
	popup_all: function (e, p2) {
		p2 = query(p2);
		$.cookie('p2', p2);

		var c1 = 'mold.php?do=Process.delete' + p2;
		var c2 = 'mold.php?do=Process.xls' + p2;
			
		var a1 = ['删除信息', 'url', 'Common.list_delete'];
		var a2 = ['导出Excel', 'url', 'jswin', '1024x768'];

		var s = c1 + ", " + c2;

		return popup(e, [a1, a2], s);
	}
};

function TypeSelectCellEditor(args) {
	var $select;
    var defaultValue;
    var scope = this;

    this.init = function() {
        $select = $("<SELECT tabIndex='0' id='type_'></SELECT>");
        
        $select.empty();
        // alert(currency_json);
        $.each(type_json, function(k, v) {
        	// alert(k + " = " + v);
        	var item = new Option(v, k);
        	$select[0].options.add(item);
        });
        
        $select.appendTo(args.container);        
        $select.focus();
    };

    this.destroy = function() {
        $select.remove();
    };

    this.focus = function() {
        $select.focus();
    };

    this.loadValue = function(item) {
    	$select.val(item[args.column.field]);
    	$select.select();
    };

    this.serializeValue = function() {
        return $select.val();
    };

    this.applyValue = function(item,state) {    	
        item[args.column.field] = state;
    };
   
    this.isValueChanged = function() {
        return ($select.val() != defaultValue);
    };

    this.validate = function() {
        return {
            valid: true,
            msg: null
        };
    };

    this.init();
}
